//
//  CFRMEndTodayAlertView.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/7.
//

import UIKit
import ProgressHUD

class CFRMEndTodayAlertView: UIView {

    @IBOutlet var percentTF: UITextField!
    @IBOutlet var weightTD: UITextField!
    @IBOutlet var nameLabel: UILabel!
    @IBOutlet var recipeButton: UIButton!
    var clickSaveBlock: (() -> Void)?

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    @IBAction func clickRecipeButtonAction(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    var model: CFRMRecipeHistoryModel? {
        didSet {
            self.nameLabel.text = self.model?.recipeName
        }
    }
    @IBAction func clickRecipeAction(_ sender: Any) {
//        clickRecipeButtonAction(self.recipeButton)
        
    }
    @IBAction func clickSaveAction(_ sender: Any) {
        self.endEditing(true)
        if self.weightTD.text!.isEmpty {
            ProgressHUD.showError("Please enter today weight")
            return
        }
        if self.percentTF.text!.isEmpty {
            ProgressHUD.showError("Please enter body fat percentage")
            return
        }
        self.model?.weight = self.weightTD.text!
        self.model?.percentage = self.percentTF.text!
        self.model?.isDefault = false
        self.model?.saveToDB()
        ProgressHUD.showSuccess("Today fat reducing meal addition succeed")
//        let helper = CFRMRecipeHistoryModel.getUsingLKDBHelper()
//        helper.delete(with: CFRMRecipeHistoryModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) { [self] in
            self.removeFromSuperview()
            clickSaveBlock?()
        }
    }
    @IBAction func clickCloseAction(_ sender: Any) {
        self.removeFromSuperview()
    }
}
